

from . import models
import datetime

# Create your views here.

class ProductType:

    def get(self):

        result_list = []

        result = models.ProductType.objects.all()

        for i in result:

            result_list.append({"id": i.id, "product_type": i.product_type})

        return {
                "status": 1,
                "errorCode": None,
                "errorMsg": None,
                "resultBody": result_list
            }
    

    def post(self, product_type):

        p = models.ProductType(product_type=product_type)

        p.save()

        return {
                "status": 1,
                "errorCode": None,
                "errorMsg": None,
                "resultBody": p.id
            }
        


class ProductTypeDetail():


    def get(self, pk):

        result = models.ProductType.objects.get(id=pk)

        return {
                "status": 1,
                "errorCode": None,
                "errorMsg": None,
                "resultBody": {
                    'id': result.id,
                    'product_type': result.product_type
                }
            }
        

    def post(self, pk, product_type): # 修改

        res = models.ProductType.objects.get(id=pk)

        res.product_type = product_type

        res.save()

        return  {
                "status": 1,
                "errorCode": None,
                "errorMsg": None,
                "resultBody": "修改成功"
            }

    def delete(self, pk): # 删除

        res = models.ProductType.objects.get(id=pk)

        res.delete()

        return {
                "status": 1,
                "errorCode": None,
                "errorMsg": None,
                "resultBody": "删除成功"
            }



def tasks_in_progress(): # 安任务类型取出正在进行的任务数量

    result = {}

    product_type = models.ProductType.objects.all()

    for i in product_type:

        result[i.id] = {"id": i.id, "product_type":i.product_type, "tasks_in_progress_count": models.Product.objects.filter(operation=2, product_type__id=i.id).count()}



    
    return {
        "status": 1,
        "errorCode": None,
        "errorMsg": None,
        "resultBody": result
    }

def get_product_by_type(product_type_id, operation=None): # 按任务类型取出product

    result = []

    if operation == None:


        product_list = models.Product.objects.filter(product_type__id=product_type_id)
    
    else:

        product_list = models.Product.objects.filter(product_type__id=product_type_id, operation=operation)

    for product in product_list:

        status = None

        if product.operation == 2:

            if product.end_date.replace(tzinfo=None) < datetime.datetime.utcnow(): # 在进行的任务超时未完成

                status = 0

            elif product.end_date.replace(tzinfo=None) < datetime.datetime.utcnow() + datetime.timedelta(days=1): # 在进行的任务还省不到1天时间

                status = 1

            elif product.end_date.replace(tzinfo=None) < datetime.datetime.utcnow() + datetime.timedelta(days=3): # 在进行的任务还省不到3天时间
                
                status = 3


        info = {
            "id": product.id,
            "product": product.product,
            "product_code": product.product_code,
            "master": product.master.username,
            "goal": product.goal,
            "product_backlog_count": models.ProductBacklog.objects.filter(product__id=product.id).count(),
            "product_backlog_count_success_count": models.ProductBacklog.objects.filter(product__id=product.id, operation=3).count(),
            "product_backlog_count_in_progress": [i.product_backlog for i in models.ProductBacklog.objects.filter(product__id=product.id, operation=2)],
            "completion_date": product.completion_date,
            "start_date": product.start_date,
            "end_date": product.end_date,
            "status": status,
        }

        result.append(info)

    return {
        "status": 1,
        "errorCode": None,
        "errorMsg": None,
        "resultBody": result
    }




def get_product_by_id(product_id): # 获取 product

    product = models.Product.objects.get(id=product_id)

    info = {
            "id": product.id,
            "product": product.product,
            "product_code": product.product_code,
            "master": product.master.username,
            "goal": product.goal,
            "completion_date": product.completion_date,
            "operation": product.operation
        }
    
    return {
        "status": 1,
        "errorCode": None,
        "errorMsg": None,
        "resultBody": info
    }


def get_product_backlog_by_product_id(product_id): # 

    result = []

    product_backlog_list = models.ProductBacklog.objects.filter(product__id=product_id)

    for product_backlog in product_backlog_list:

        status = None

        if product_backlog.operation in [1, 2]:

            if product_backlog.end_date.replace(tzinfo=None) < datetime.datetime.utcnow(): # 在进行的任务超时未完成

                status = 0

            elif product_backlog.end_date.replace(tzinfo=None) < datetime.datetime.utcnow() + datetime.timedelta(days=1): # 在进行的任务还省不到1天时间

                status = 1

            elif product_backlog.end_date.replace(tzinfo=None) < datetime.datetime.utcnow() + datetime.timedelta(days=3): # 在进行的任务还省不到3天时间
                
                status = 3

        info = {
            "id": product_backlog.id,
            "goal": product_backlog.goal,
            "product_backlog": product_backlog.product_backlog,
            "responsible": product_backlog.responsible.username,
            "start_date": product_backlog.start_date,
            "end_date": product_backlog.end_date,
            "completion_date": product_backlog.completion_date,
            "operation": product_backlog.operation,
            "sprint_backlog_count": models.SprintBacklog.objects.filter(product_backlog__id=product_backlog.id).count(),
            "sprint_backlog_count_success_count": models.SprintBacklog.objects.filter(product_backlog__id=product_backlog.id, operation=3).count(),
            "status": status,
        }

        result.append(info)

    return {
        "status": 1,
        "errorCode": None,
        "errorMsg": None,
        "resultBody": result
    }

def get_sprint_backlog_by_product_backlog_id(product_backlog_id): # 

    result = []

    sprint_backlog_list = models.SprintBacklog.objects.filter(product_backlog__id=product_backlog_id)

    for sprint_backlog in sprint_backlog_list:

        info = {
            "id": sprint_backlog.id,
            "sprint_backlog": sprint_backlog.sprint_backlog,
            "start_date": sprint_backlog.start_date,
            "end_date": sprint_backlog.end_date,
            "completion_date": sprint_backlog.completion_date,
            "operation": sprint_backlog.operation,

        }

        result.append(info)

    return {
        "status": 1,
        "errorCode": None,
        "errorMsg": None,
        "resultBody": result
    }




